SPORE.$Import('core.object');
SPORE.$Import('core.class.extra');
SPORE.$Import('core.element.base');

/*
---

name: Swiff

description: Wrapper for embedding SWF movies. Supports External Interface Communication.

license: MIT-style license.

credits:
  - Flash detection & Internet Explorer + Flash Player 9 fix inspired by SWFObject.

requires: [Options, Object, Element]

provides: Swiff

...
*/

SPORE.register('core.swiff',function($ns){

	var Swiff = this.Swiff = new Class({
	
		Implements: Options,
	
		options: {
			id: null,
			height: 1,
			width: 1,
			container: null,
			properties: {},
			params: {
				quality: 'high',
				allowScriptAccess: 'always',
				wMode: 'window',
				swLiveConnect: true
			},
			callBacks: {},
			vars: {}
		},
	
		toElement: function(){
			return this.object;
		},
	
		initialize: function(path, options){
			this.instance = 'Swiff_' + String.uniqueID();
	
			this.setOptions(options);
			options = this.options;
			var id = this.id = options.id || this.instance;
			var container = document.id(options.container);
	
			Swiff.CallBacks[this.instance] = {};
	
			var params = options.params, vars = options.vars, callBacks = options.callBacks;
			var properties = Object.append({height: options.height, width: options.width}, options.properties);
	
			var self = this;
	
			for (var callBack in callBacks){
				Swiff.CallBacks[this.instance][callBack] = (function(option){
					return function(){
						return option.apply(self.object, arguments);
					};
				})(callBacks[callBack]);
				vars[callBack] = 'Swiff.CallBacks.' + this.instance + '.' + callBack;
			}
	
			params.flashVars = Object.toQueryString(vars);
			if (Browser.ie){
				properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
				params.movie = path;
			} else {
				properties.type = 'application/x-shockwave-flash';
			}
			properties.data = path;
	
			var build = '<object id="' + id + '"';
			for (var property in properties) build += ' ' + property + '="' + properties[property] + '"';
			build += '>';
			for (var param in params){
				if (params[param]) build += '<param name="' + param + '" value="' + params[param] + '" />';
			}
			build += '</object>';
			this.object = ((container) ? container.empty() : new Element('div')).set('html', build).firstChild;
		},
	
		replaces: function(element){
			element = document.id(element, true);
			element.parentNode.replaceChild(this.toElement(), element);
			return this;
		},
	
		inject: function(element){
			document.id(element, true).appendChild(this.toElement());
			return this;
		},
	
		remote: function(){
			return Swiff.remote.apply(Swiff, [this.toElement()].append(arguments));
		}
	
	});
	
	Swiff.CallBacks = {};
	
	Swiff.remote = function(obj, fn){
		var rs = obj.CallFunction('<invoke name="' + fn + '" returntype="javascript">' + __flash__argumentsToXML(arguments, 2) + '</invoke>');
		return eval(rs);
	};

});